package xiegd.study.source;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import xiegd.study.source.Mapping.UserInfoMapper;
import xiegd.study.source.entity.UserInfo;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;

public class mybatisMain {

    private  SqlSession sqlSession;
    @BeforeEach
    public  void beReady(){
        String resource = "mybatis.xml";
        System.out.println("我被启动了；；");
        Reader reader;
        try {
            reader = Resources.getResourceAsReader(resource);
            SqlSessionFactory  sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
             sqlSession = sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void addTest(){
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo user = new UserInfo();
        user.setId(666);
        user.setUsername("me");
        user.setLikes(new ArrayList<Integer>(){{add(4);add(5);add(8);}});
        int i = userInfoMapper.addOne(user);
        System.out.println("i:::" +i +",,," +user);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void selectOne(){
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = userInfoMapper.findOne(1);
        System.out.println(userInfo);
        sqlSession.close();
    }
}
